package com.xuecheng.content.controller;

import com.xuecheng.api.content.TeachplanApi;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.api.content.model.dto.TeachplanMediaDTO;
import com.xuecheng.api.content.model.vo.BindTeachplanMediaVO;
import com.xuecheng.api.content.model.vo.TeachplanVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程计划 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping("teachplan")
public class TeachplanController implements TeachplanApi {

    @Autowired
    private TeachplanService  teachplanService;


    /**
     * 根据课程id查询课程计划的树形结构，即三级目录
     *
     * @param courseId 课程基础id值
     * @return com.xuecheng.api.content.model.dto.TeachplanDTO
     * @author Lily
     * @date 2021/12/12 19:56
     * @paramType [java.lang.Long]
     * @description
     * 参数一定要打注解
     * 业务分析：
     * 1.传递公司的id
     */
    @GetMapping("/{courseId}/tree-nodes")
    @Override
    public TeachplanDTO queryTreeNodesByCourseId(@PathVariable Long courseId) {
        // 1.传递公司的id
        Long companyId = SecurityUtil.getCompanyId();
        TeachplanDTO teachplanDTO = teachplanService.queryTreeNodesByCourseId(courseId, companyId);
        return teachplanDTO;
    }


    /**
     * 创建或修改教学计划
     *
     * @param teachplanVO 课程计划VO
     * @return com.xuecheng.api.content.model.dto.TeachplanDTO
     * @author Lily
     * @date 2021/12/12 21:42
     * @paramType [com.xuecheng.api.content.model.vo.TeachplanVO]
     * @description
     *
     * 1.获得公司id
     * 2.将vo转为dto
     * 3.调用service处理业务，返回dto
     *
     * 参数是请求体，因此要加 RequestBody
     */
    @PostMapping("")
    @Override
    public TeachplanDTO createOrModifyTeachPlan(@RequestBody TeachplanVO teachplanVO) {

        // 1.获得公司id
        Long companyId = SecurityUtil.getCompanyId();

        // 2.将vo转为dto
        TeachplanDTO teachplanDTO = TeachplanConvert.INSTANCE.vo2dto(teachplanVO);

        // 3.调用service处理业务，返回dto
        TeachplanDTO result = teachplanService.createOrModifyTeachPlan(teachplanDTO, companyId);

        return result;
    }


    /**
     * 删除教学计划
     *
     * @param teachPlanId 教学计划id
     * @author Lily
     * @date 2021/12/13 10:32
     * @paramType [java.lang.Long]
     * @description
     *
     *
     */
    @DeleteMapping("")
    @Override
    public void removeTeachPlan(@RequestParam("teachPlanId") Long teachPlanId) {
        Long companyId = SecurityUtil.getCompanyId();
        teachplanService.removeTeachPlan(teachPlanId, companyId);
    }

    /**
     * @author Lily
     * @date 2021/12/20 23:31
     * @paramType [com.xuecheng.api.content.model.vo.BindTeachplanMediaVO]
     * @param vo
     * @return com.xuecheng.api.content.model.dto.TeachplanMediaDTO
     * @description 课程计划绑定媒资信息
     */
    @Override
    @PostMapping("teachplan/media/association")
    public TeachplanMediaDTO associateMedia(@RequestBody BindTeachplanMediaVO vo) {
        Long companyId = SecurityUtil.getCompanyId();
        TeachplanMediaDTO dto = new TeachplanMediaDTO();
        dto.setMediaId(vo.getMediaId());
        dto.setTeachplanId(vo.getTeachplanId());
        TeachplanMediaDTO teachplanMediaDTO = teachplanService.associateMedia(dto, companyId);

        return teachplanMediaDTO;
    }
}
